/*
 * adcPacketHandler.h
 *
 * Created: 6/13/2013 3:01:16 AM
 *  Author: Tim
 */ 


#ifndef ADCPACKETHANDLER_H_
#define ADCPACKETHANDLER_H_

#include <avr/io.h>
#include "../status.h"
#include "../structs/adcPacketStructs.h"
#include "../CRC.h"
#include "../../features/tmr/tmr.h"
#include "../structs/msgHeaderDefs.h"
#include "../../features/adc/adc.h"
#include "../../communication/comPort.h"
#include "genPacketHandler.h"

static inline status_t adcInitHandler(adcInit_t *packet)
{	
	
	//need to check to see if gain is set to 0 which is used to unalloc a channel
	
	if (packet->PARAM1.GAIN == 0x7)
	{
		if(packet->PARAM1.ACHM & ADC_CH0_bp)sendGenAck(adcInit_mh,(ADC_CH0_bp)<<4 | 0x08);
		if(packet->PARAM1.ACHM & ADC_CH1_bp)sendGenAck(adcInit_mh,(ADC_CH1_bp)<<4 | 0x08);
		if(packet->PARAM1.ACHM & ADC_CH2_bp)sendGenAck(adcInit_mh,(ADC_CH2_bp)<<4 | 0x08);
		if(packet->PARAM1.ACHM & ADC_CH3_bp)sendGenAck(adcInit_mh,(ADC_CH3_bp)<<4 | 0x08);
		return OPP_FAILED;
	}
	else if( packet->PARAM1.GAIN == 0)
	{
		//turn off all channels that are called out here;
		if(packet->PARAM1.ACHM & ADC_CH0_bp)
		{
			if(daqAlloc.analogAlloc.analogInCh0Alloc == FEATURE_UTILIZED)
			{
				//if the channel is turned off we need to change the inputSuperPacket length accordingly 
				inputSuperPacketLength +=2;
			}
			
			daqAlloc.analogAlloc.analogInCh0Alloc = FEATURE_FREE;
			
			//disable the interrupt
			ADCA.CH0.INTCTRL &= ~ADC_CH_INTLVL_gm;
			sendGenAck(adcInit_mh,(ADC_CH0_bp)<<4 | packet->PARAM1.GAIN);
		}
		if(packet->PARAM1.ACHM & ADC_CH1_bp)
		{
			if(daqAlloc.analogAlloc.analogInCh1Alloc == FEATURE_UTILIZED)
			{
				//if the channel is turned off we need to change the inputSuperPacket length accordingly
				inputSuperPacketLength +=2;
			}
			
			daqAlloc.analogAlloc.analogInCh1Alloc = FEATURE_FREE;
			
			//disable the interrupt
			ADCA.CH1.INTCTRL &= ~ADC_CH_INTLVL_gm;
			sendGenAck(adcInit_mh,(ADC_CH1_bp)<<4 | packet->PARAM1.GAIN);
			
		}
		if(packet->PARAM1.ACHM & ADC_CH2_bp)
		{
			if(daqAlloc.analogAlloc.analogInCh2Alloc == FEATURE_UTILIZED)
			{
				//if the channel is turned off we need to change the inputSuperPacket length accordingly
				inputSuperPacketLength +=2;
			}
			daqAlloc.analogAlloc.analogInCh2Alloc = FEATURE_FREE;
			
			//disable the interrupt
			ADCA.CH2.INTCTRL &= ~ADC_CH_INTLVL_gm;
			sendGenAck(adcInit_mh,(ADC_CH2_bp)<<4 | packet->PARAM1.GAIN);
			
		}
		if(packet->PARAM1.ACHM & ADC_CH3_bp)
		{
			if(daqAlloc.analogAlloc.analogInCh3Alloc == FEATURE_UTILIZED)
			{
				//if the channel is turned off we need to change the inputSuperPacket length accordingly
				inputSuperPacketLength +=2;
			}
			
			daqAlloc.analogAlloc.analogInCh3Alloc = FEATURE_FREE;
			
			//disable the interrupt
			ADCA.CH3.INTCTRL &= ~ADC_CH_INTLVL_gm;
			sendGenAck(adcInit_mh,(ADC_CH3_bp)<<4 | packet->PARAM1.GAIN);
		}
		
		return OPP_SUCCESSFUL;
	}
	
	// set the adc gains of the respective channels		
	adcSetGain(packet->PARAM1.ACHM,packet->PARAM1.GAIN);
	//allocate the adc channel interrupts appropriately
	
	//10_15_13 now acks are sent for every channel that was manipulated
	if (packet->PARAM1.ACHM & ADC_CH0_bp)
	{	
		daqAlloc.analogAlloc.analogInCh0Alloc = FEATURE_UTILIZED;
		inputSuperPacketLength +=2;
		sendGenAck(adcInit_mh,(ADC_CH0_bp)<<4 | packet->PARAM1.GAIN);//send ack with the channel and gain setting
	}
	if (packet->PARAM1.ACHM & ADC_CH1_bp)
	{
		daqAlloc.analogAlloc.analogInCh1Alloc = FEATURE_UTILIZED;
		inputSuperPacketLength +=2;
		sendGenAck(adcInit_mh,(ADC_CH1_bp)<<4 | packet->PARAM1.GAIN);//send ack with the channel and gain setting
	}
	if (packet->PARAM1.ACHM & ADC_CH2_bp)
	{
		daqAlloc.analogAlloc.analogInCh2Alloc = FEATURE_UTILIZED;
		inputSuperPacketLength +=2;
		sendGenAck(adcInit_mh,(ADC_CH2_bp)<<4 | packet->PARAM1.GAIN);//send ack with the channel and gain setting
	}
	if (packet->PARAM1.ACHM & ADC_CH3_bp)
	{
		daqAlloc.analogAlloc.analogInCh3Alloc = FEATURE_UTILIZED;
		inputSuperPacketLength +=2;	
		sendGenAck(adcInit_mh,(ADC_CH3_bp)<<4 | packet->PARAM1.GAIN);//send ack with the channel and gain setting
	}
	
	
	return OPP_SUCCESSFUL;
}

#endif /* ADCPACKETHANDLER_H_ */